package com.kmist.xiakexing.data;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.utils.UserBean;

public class UserDao implements IUserDao{

    private static UserDao sInstance;
    private XiakexingDBHelper xiakexingDBHelper;
    private SQLiteDatabase db;
    private Cursor c;
    private IUserDaoCallback mCallback;
    private UserBean userBean;

    public static UserDao getInstance() {
        if (sInstance == null) {
            synchronized (UserDao.class){
                sInstance = new UserDao();
            }
        }
        return sInstance;
    }

    public UserDao() {
        xiakexingDBHelper = new XiakexingDBHelper(Config.context);
    }

    @Override
    public void setCallBack(IUserDaoCallback callBack) {
        this.mCallback = callBack;
    }

    @Override
    public void addUser(UserBean userBean) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "";
            db.execSQL(sql, new Object[]{userBean.getUserName(), userBean.getUserPass(), userBean.getToken()});

            if (mCallback != null) {
                mCallback.onUserAdded();
            }
        } catch (Exception e) {
            if (mCallback != null) {
                mCallback.onUserAddErr();
            }
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    @Override
    public UserBean getUser(UserBean userBeanTmp) {
        userBean = new UserBean();
        try {
            db = xiakexingDBHelper.getReadableDatabase();
            c = db.query(Config.TABLE_NAME_USERINFO, null, "username=? and userpass = ?", new String[]{userBeanTmp.getUserName(), userBeanTmp.getUserPass()}, null, null,null);
            if(c.moveToFirst()){
                do{
                    userBean.setUserName(c.getString(c.getColumnIndex("username")));
                    userBean.setUserPass(c.getString(c.getColumnIndex("userpass")));
                    userBean.setToken(c.getString(c.getColumnIndex("token")));
                }while (c.moveToNext());
                if (mCallback != null) {
                    mCallback.onUserInfoLoaded(userBean);
                }
                return userBean;
            }else{
                if(mCallback != null){
                    mCallback.onUserInfoLoadEmpty();
                }
            }
        } catch (Exception e) {
            if(mCallback != null){
                mCallback.onUserInfoLoadError();
            }
            e.printStackTrace();
        }finally {
            c.close();
            db.close();
        }
        return null;
    }

    @Override
    public String isUserOnline(String token) {
        String sid = null;
        try {
            db = xiakexingDBHelper.getReadableDatabase();
            c = db.query(Config.TABLE_NAME_GAME, null, "token=?", new String[]{token}, null, null,null);
            if(c.moveToFirst()){
                do{
                    sid = c.getString(c.getColumnIndex("sid"));
                }while (c.moveToNext());
                return sid;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            c.close();
            db.close();
        }
        return null;
    }

    @Override
    public boolean updateUserOnline(String sid) {
        try {
            db = xiakexingDBHelper.getWritableDatabase();
            String sql = "update game set endtime = ? , sfzx=1 where sid = ?";
            db.execSQL(sql, new Object[]{Config.data(), sid});
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.close();
        }
        return false;
    }
}
